package com.itheima.demo01;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class RunnableSocket implements Runnable {
    private Socket accept;

    public RunnableSocket(Socket accept) {
        this.accept = accept;
    }

    @Override
    public void run() {
        BufferedOutputStream bos = null;
        try {
            BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
            String fileName = UUID.randomUUID().toString().replace("-", "") + ".jpg";
            bos = new BufferedOutputStream(new FileOutputStream("day17_practice\\bbb\\" + fileName));
            int b;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            String s = "上传成功！";
            bw.write(s);
            bw.newLine();
            bw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (accept != null) {
                    accept.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
